package com.study.talk.common.config;

import com.google.common.base.Joiner;
import org.springframework.boot.autoconfigure.cache.CacheManagerCustomizer;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

@Configuration
@EnableCaching
@Profile("cacheKey")
public class CacheConfig extends CachingConfigurerSupport {

    @Bean
    public CacheManagerCustomizer<ConcurrentMapCacheManager> cacheManagerCustomizer() {
        return new CacheManagerCustomizer<ConcurrentMapCacheManager>() {
            @Override
            public void customize(ConcurrentMapCacheManager cacheManager) {
                cacheManager.setAllowNullValues(false);
            }
        };
    }
    @Override
    public KeyGenerator keyGenerator() {
        return (o,m,args)->{
            StringBuilder builder = new StringBuilder();
            builder.append(o.getClass().getName()).append("#").append(m.getName()).append("(");
            Joiner joiner = Joiner.on(",").skipNulls();
            String arg = joiner.join(args);
            builder.append(arg).append(")");
            return builder.toString();
        };
    }
}
